﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Doo_JetDAL;
using DoodJetView.Core.Reservation;
using DoodJetView.VuesPartielles.Reservation;
using DoodJetView.Core;

namespace DoodJetView.VuesPartielles
{
    /// <summary>
    /// Logique d'interaction pour ReservationCreation.xaml
    /// </summary>
    public partial class ReservationCreation : UserControl
    {
        private MainWindow mainWindow;
        private reservation r;

        public ReservationCreation(MainWindow Main)
        {
            InitializeComponent();
            
            MainWindows = Main;
            LoadDataForForm();
            UpdateListClient();
            rempliCmbDuree();
        }

        private void UpdateListClient()
        {
            using(DataEntities db = new DataEntities()){
                List<client> liste = db.client.OrderBy(o=>o.nomClient).ToList();
                ListClient.Items.Clear();

                foreach (client item in liste)
                {
                    ListClient.Items.Add(new ElementListPoco { id = (int)item.id_client, text = item.nomClient + " " + item.prenomClient + " (" + DateTimeUnixTools.ConvertFromUnixTimestamp( item.dateNaissance)});
                }
            }
        }
        private void LoadDataForForm(){
            //Chargement des Activités 
            using (DataEntities db = new DataEntities())
            {
                List<TypeEquipement> equipements =  db.TypeEquipement.ToList();
                foreach (TypeEquipement t in equipements)
                {
                    var Poco = new ElementListPoco { id = (int)t.id_TypeEquipement, text = t.libelle };
                    this.listBox_Activites2.Items.Add(Poco);
                }
            
            }
        }
        public MainWindow MainWindows { get; set; }

        /// <summary>
        /// Déclenche le rafrachissement de la vue!
        /// </summary>
        private void ActualiseResultat() 
        {
            this.ViewResultat = new DynamicGridResultat(GridResultat);
            ViewResultat.Render();
        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            PartialDayOfActivities prt = new PartialDayOfActivities("test", this);
           // this.TabsPos.Items.Clear();
            var item = this.MainWindows.ContentGrid.Children[0] as ReservationCreation;
            //item.DayByDay.Children.Add(prt);
        }

        internal DynamicGridResultat ViewResultat { get; set; }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ActualiserResultats();
            
        }

        private void ActualiserResultats()
        {
            //ActualiseResultat();
            GestionReservation.reservationCreation = this;

            //MessageBox.Show("Date : " + (new DateTime()).AddHours(12).ToString("dd/MM/yyyy hh:mm:ss"));
            try
            {
                GridResultat.Children.Clear();
                DynamicGridTools tmpres = GestionReservation.verifieUneReservetion(((ElementListPoco)(listBox_Activites2.SelectedItem)).id, (DateTime)Picker_DateDebut.SelectedDate, this);
                GridResultat.Children.Add(tmpres.DynamicGrid);

                /*   GestionReservation.verifieUneReservetion(
                                   ((ElementListPoco)(cmb_Duree.SelectedItem)).id, 
                                   ((ElementListPoco)(ListClient.SelectedItem)).id,
                                   ,
                                   (DateTime)Picker_DateDebut.SelectedDate); */
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //throw;
            }
        }

        private void rempliCmbDuree() {
            cmb_Duree.Items.Clear();
            cmb_Duree.Items.Add(new ElementListPoco { id = 10, text = "10 min" });
            cmb_Duree.Items.Add(new ElementListPoco { id = 15, text = "15 min" });
            cmb_Duree.Items.Add(new ElementListPoco { id = 20, text = "20 min" });
            cmb_Duree.Items.Add(new ElementListPoco { id = 30, text = "30 min" });
            cmb_Duree.Items.Add(new ElementListPoco { id = 40, text = "40 min" });
            cmb_Duree.Items.Add(new ElementListPoco { id = 45, text = "45 min" });
            cmb_Duree.Items.Add(new ElementListPoco { id = 50, text = "50 min" });
            cmb_Duree.Items.Add(new ElementListPoco { id = 60, text = "60 min" });
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            if( Picker_DateDebut.SelectedDate!= null) {
                using (DataEntities db = new DataEntities())
                {
                    try
                    {
                        int bases = DateTimeUnixTools.ConvertToUnixTimestamp((DateTime)Picker_DateDebut.SelectedDate);
                        reservation r = new reservation();
                        r.id_client = ((ElementListPoco)(ListClient.SelectedItem)).id;
                        r.dateDebut = bases  + (Int32.Parse(heure.Text) * 60) + Int32.Parse(min.Text);
                        r.dateFin = bases + (Int32.Parse(heure.Text) * 60) + Int32.Parse(min.Text) + ((ElementListPoco)(cmb_Duree.SelectedItem)).id;
                        r.NbDePersonnes = (long)Int32.Parse(NbPerso.Text);
                        db.reservation.AddObject(r);
                        db.SaveChanges();
                        float ideq = float.Parse(valEquipe.Text);
                        equipements q = db.equipements.Single(o => o.id_equipement == ideq);
                        r.equipements.Add(q);
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    { MessageBox.Show("Une erreur est présente sur le formulaire"); }
                }
                ActualiserResultats();
            }

        }



    }
}
